package common.zsxmlv.utils;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: ZSXM
 * @Description: 企业微信相关
 * @Company: DingZheng
 * @Date: Create by 2018/6/10 10:09
 * @Version: 1.0
 * @Modify by:
 */
public class WeiXinWorkUtils {

    private static Logger log = LoggerFactory.getLogger(WeiXinWorkUtils.class);

    private static final String corpid = "ww6d4fa42efc7d4377";
    private static final String corpsecret = "cIH775_GxyaSz1GeGBUqWa9uEzpivAOgcMtlsFzK5P0";

    /**
     * 获取accessToken
     * @return accessToken
     */
    public static String getAccessToken() {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        try {
            String s = HttpUtils.get(url);
            if (s != null && !"".equals(s)) {
                JSONObject jsonObject = JSONObject.fromObject(s);
                return jsonObject.get("access_token").toString();
            } else {
                log.error("【accessToken获取失败】");
            }
        } catch (Exception e) {
            log.error("【获取accessToken异常】,error={}", e.getMessage());
        }
        return "";
    }

    /**
     * 获取部门成员
     * @param departId 部门ID
     * @param fetch_child 是否递归获取子部门下面的成员
     * @return "{'errcode':0,'errmsg':'ok','userlist':[{'userid':'ShangKai','name':'尚凯','department':[1]},{}]}"
     */
    public static String getUserListByDepartId(Integer departId, Integer fetch_child) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + getAccessToken() + "&department_id=" + departId + "&fetch_child=" + fetch_child;
        try {
            return HttpUtils.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String uploadMedia() {
        return corpid;
    }
}
